<template>
  <el-row class="RangeDrawer">
    <commonDrawer
      title="客服账号设置"
      :drawer="drawer"
      :size="'600px'"
      :isShowMoreBtn="true"
      @closeDraw="closeDraw"
      @cancelFun="closeDraw"
      @save="closeDraw"
    >
      <template slot="column">
        <div class="range-area" v-loading="loading">
          <div class="serviceItem" v-for="item, index in data" :key="index">
            <img :src="item.avatar" />
            <div class="text">{{ item.name }}<span>@{{ companyName }}</span></div>
            <div class="icongroup">
              <el-tooltip class="item" effect="dark" content="点击复制客服链接" placement="top">
                <i class="V7_iconfont V7_iconV7tubiao_fuzhi" @click="copy(item.kfURL)"></i>
              </el-tooltip>
              <i class="V7_iconfont V7_iconV7tubiao_xianbianji" @click="edit(item)" v-functions="['wxkf_access_edit']"></i>
              <i class="V7_iconfont V7_iconV7tubiao_shanchu" @click="deleteItem(item)" v-functions="['wxkf_access_delete']"></i>
            </div>
          </div>
          <div>
            <el-button v-if="data.length < 10" type="text" @click="add" v-functions="['wxkf_access_add']"><i class="V7_iconfont V7_iconV7tubiao_xinzeng" style="font-size: 12px"></i>创建客服账号</el-button>
          </div>
        </div>
      </template>
    </commonDrawer>
    <EditService ref="editService" @getData="getData"></EditService>
  </el-row>
</template>

<script lang="ts">
import { Component, Vue, Ref } from 'vue-property-decorator';
import commonDrawer from '@/common/views/drawer/index.vue';
import { commonAction, deepClone } from '@/utils/common';
import Clipboard from 'clipboard-js';
import EditService from './EditService.vue'
import { channelConfigMoudle } from '@/store/modules/config/channel/index'
const accessFormTem = {
  name: '',
  processId: '',
  isSatisfaction: true,
  avatar: '',
  satisfactionId: ''

}
@Component({
  name: 'ServiceDrawer',
  components: {
    commonDrawer,
    EditService
  }
})
export default class ServiceDrawer extends Vue {
  [x: string]: any
  @Ref() private readonly tree: any
  @Ref() private readonly editService: any
  private drawer: boolean = false
  private data: any = []
  private companyName: string = ''
  private corpId: string = ''
  private satisfactionId: string = ''

  private loading: boolean = false

  private mounted () {
    const actionData = { url: '/im-center/action/config/satisfaction/list', method: 'POST', data: {} }
    channelConfigMoudle.channelCommonAction(actionData).then(res => {
      if (res && res.success && res.data && res.data.list) {
        const currentObj = res.data.list.filter(item => {
          return item.isDefault
        })
        this.satisfactionId = currentObj[0]._id
      }
    })
  }

  private async getData () {
    this.loading = true;
    try {
      const params = { url: '/im-wechat/action/config/wxkfAuthMode/getChannelList', method: 'POST', data: { corpId: this.corpId } }
      const result: any = await commonAction(params)
      if (result.success) {
        this.data = result.data.list
      }
      this.loading = false
    } catch (e) {
      this.loading = false
    }
  }

  private async openDraw (data: any) {
    this.drawer = true;
    this.companyName = data.name
    this.corpId = data.corpId
    this.getData()
  }

  private closeDraw () {
    this.drawer = false;
    this.data = []
    this.companyName = ''
    this.corpId = ''
  }

  private copy (data: any) {
    if (data) {
      Clipboard.copy(data).then(() => {
        this.$message.success('复制成功')
      }).catch(() => {
        this.$message.error('复制失败')
      })
    }
  }

  private edit (data: any) {
    this.editService.openDraw({ ...data, satisfactionId: data.satisfactionId || this.satisfactionId }, 'edit')
  }

  private add () {
    this.editService.openDraw({ ...accessFormTem, corpId: this.corpId, satisfactionId: this.satisfactionId }, 'add')
  }

  private deleteItem (data: any) {
    this.$confirm('删除后，用户将无法发起咨询，如果咨询入口已正式对外，建议先下掉咨询入口再删除账号', '删除确认', {
      confirmButtonText: '继续',
      cancelButtonText: '取消',
      type: 'error'
    }).then(async () => {
      const params = { url: '/im-wechat/action/config/wxkfAuthMode/deleteChannel', method: 'POST', data: { _id: data._id } }
      const result: any = await commonAction(params)
      if (result.success) {
        this.$message.success('删除成功')
        this.getData()
      }
    })
  }
}
</script>

<style lang="stylus" scoped>
.RangeDrawer
  .range-area
    position absolute
    height calc(100% - 132px)
    width calc(100% - 48px)
    overflow auto
    .serviceItem
      height: 64px;
      border: 1px solid #E7E9ED;
      border-radius: 4px;
      padding: 16px 12px
      margin-bottom: 16px
      img
        width: 32px;
        height: 32px;
        border-radius: 50%;
        float: left
      .text
        width: calc(100% - 150px)
        line-height: 32px
        padding-left: 12px
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #303753;
        float: left
        span
          color $c-main
      .icongroup
        float: right
        line-height: 32px
        i
          font-size: 12px
          color: #B3B9C7
          margin-left: 16px
          cursor pointer
        i:hover
          color $c-main
</style>
